package cn.tedu.charging.user.dao.repository;

import cn.tedu.charging.common.pojo.param.ChargeParam;
import cn.tedu.charging.common.pojo.po.ChargingUserInfoPO;
import cn.tedu.charging.user.dao.mapper.ChargingUserInfoMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UserRepository {
    @Autowired
    private ChargingUserInfoMapper chargingUserInfoMapper;

    public ChargingUserInfoPO getUserByOpenId(String openId) {
        //select * from charging_user_info where wx_open_id = #{openId}
        QueryWrapper<ChargingUserInfoPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wx_open_id",openId);
        return chargingUserInfoMapper.selectOne(queryWrapper);
    }

    public void save(ChargingUserInfoPO userInfoPO) {
        chargingUserInfoMapper.insert(userInfoPO);
        //insert into charging_user_info values(null,#{wxOpenId},#{nickName},#{balance},#{username},#{gender},#{age},#{occupation},#{password},#{phone},#{createTime},#{updateTime},#{deleted})
    }

    public ChargingUserInfoPO getUserById(Integer userId) {
        return chargingUserInfoMapper.selectById(userId);
    }


    public void updateBalanceByUserId(ChargeParam param) {
        //update charging_user_info set balance = balance + #{amount} where id = #{userId}
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",param.getUserId());
        updateWrapper.setSql("balance = balance + "+param.getAmount());
        chargingUserInfoMapper.update(null,updateWrapper);
    }
}
